import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'


function checkRoute(path, next) {
  if (store.state.user.menus_url.includes(path)) {
    next()
  } else {
    next('/index/home')
  }
}
Vue.use(VueRouter)
export const indexRouters = [
  {
    path: 'menu',
    component: () => import('../pages/menu/menu.vue'),
    meta: {
      title: '菜单管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/menu', next)
    }
  },
  {
    path: 'manger',
    component: () => import('../pages/manger/manger.vue'),
    meta: {
      title: '管理员管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/manger', next)
    }
  },
  {
    path: 'people',
    component: () => import('../pages/people/people.vue'),
    meta: {
      title: '角色管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/people', next)
    }
  },
  {
    path: 'banner',
    component: () => import('../pages/banner/banner.vue'),
    meta: {
      title: '轮播图管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/banner', next)
    }
  },
  {
    path: 'seckill',
    component: () => import('../pages/seckill/seckill.vue'),
    meta: {
      title: '秒杀管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/seckill', next)
    }
  },
  {
    path: 'shopCate',
    component: () => import('../pages/shopCate/shopCate.vue'),
    meta: {
      title: '商品分类'
    },
    beforeEnter(to, from, next) {
      checkRoute('/shopCate', next)
    }
  },
  {
    path: 'shopSpecs',
    component: () => import('../pages/shopSpecs/shopSpecs.vue'),
    meta: {
      title: '商品规格'
    },
    beforeEnter(to, from, next) {
      checkRoute('/shopSpecs', next)
    }
  },
  {
    path: 'vipManger',
    component: () => import('../pages/vipManger/vipManger.vue'),
    meta: {
      title: '会员管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/vipManger', next)
    }
  },
  {
    path: 'shopGoods',
    component: () => import('../pages/shopGoods/shopGoods.vue'),
    meta: {
      title: '商品管理'
    },
    beforeEnter(to, from, next) {
      checkRoute('/shopGoods', next)
    }
  },
]
const routes = [
  {
    path: '/login',
    component: () => import('../pages/login/login.vue')
  },
  {
    path: '/index',
    component: () => import('../pages/index/index.vue'),
    children: [
      {
        path: "/index/home",
        component: () => import("../pages/home/home.vue")
      },
      ...indexRouters
    ]
  },
  {
    path: '*',
    redirect: '/login'
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next();
    return
  }
  if (store.state.user.token) {
    next();
    return
  }
  next('/login')
})


export default router
